package com.phrp.service.impl;

import com.phrp.exception.CustomizationException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phrp.entity.Opinion;
import com.phrp.dao.OpinionDao;
import com.phrp.entity.vo.OpinionSearchVO;
import com.phrp.entity.vo.PageData;
import com.phrp.service.OpinionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.phrp.util.returnResult.ResponseResult;
import com.phrp.util.returnResult.ReturnInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ssc
 * @since 2024-04-06
 */
@Service
public class OpinionserviceImpl extends ServiceImpl<OpinionDao, Opinion> implements OpinionService {

    @Autowired
    private OpinionDao opinionDao;

    /**
     * 根据type获取用户数据
     *
     * @return
     */
    @Override
    public ResponseResult getOpinionData() {
        QueryWrapper<Opinion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("adopt", "1")
                .select("o_uuid", "dictionary_query('opinion','type',type) type", "title", "content",
                        "u_uuid", "important", "adopt", "create_time", "update_time")
                .orderByDesc("create_time");

        List<Opinion> opinions = opinionDao.selectList(queryWrapper);
        Long total = opinionDao.selectCount(new QueryWrapper<Opinion>().eq("adopt", "1"));

        PageData<List<Opinion>> pageData = new PageData<>();
        pageData.setTotalNumber(total);
        pageData.setData(opinions);

        return new ResponseResult(ReturnInfoEnum.SUCCESS, pageData);
    }

    /**
     * 更新数据
     *
     * @param data
     * @return
     */
    @Override
    public ResponseResult updateOpinion(Opinion data) {
        UpdateWrapper<Opinion> update = new UpdateWrapper<>();
        update.eq("o_uuid", data.getOUuid())
                .set("adopt", data.getAdopt())
                .set("important", data.getImportant());

        int i = opinionDao.update(null, update);
        if (i != 1) {
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }

    /**
     * 根据条件搜索相关内容
     *
     * @param searchData
     * @return
     */
    @Override
    public ResponseResult searchData(OpinionSearchVO searchData) {
        if (searchData.getCreateTime().size() > 0) {
            // 解析并转换第一个时间
            LocalDateTime startDateTime = LocalDateTime.parse(searchData.getCreateTime().get(0), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setStartTime(startDateTime.toLocalDate().toString());

            // 解析并转换第二个时间
            LocalDateTime endDateTime = LocalDateTime.parse(searchData.getCreateTime().get(1), DateTimeFormatter.ISO_DATE_TIME);
            searchData.setEndTime(endDateTime.toLocalDate().toString());
        }

        QueryWrapper<Opinion> query = new QueryWrapper<>();
        query.select("o_uuid", "dictionary_query('opinion','type',type) type", "title", "content",
                "u_uuid", "dictionary_query('opinion','important',important) important",
                "dictionary_query('opinion','adopt',adopt) adopt", "create_time", "update_time");
        if (searchData.getType() != "" && searchData.getType() != null) {
            query.eq("type", searchData.getType());
        }
        if (searchData.getAdopt() != "" && searchData.getAdopt() != null) {
            query.eq("adopt", searchData.getAdopt());
        }
        if (searchData.getImportant() != "" && searchData.getImportant() != null) {
            query.eq("important", searchData.getImportant());
        }
        if (searchData.getCreateTime().size() > 0) {
            query.between("create_time", searchData.getStartTime(), searchData.getEndTime());
        }
        query.orderByDesc("important")
                .orderByDesc("create_time");

        Page<Opinion> page = new Page<>();
        page.setCurrent(searchData.getCurrentPage());
        page.setSize(searchData.getPageSize());

        Page<Opinion> opinionPage = opinionDao.selectPage(page, query);

        PageData<Object> pageData = new PageData<>();

        pageData.setData(opinionPage.getRecords());
        pageData.setTotalNumber(opinionPage.getTotal());

        return new ResponseResult(ReturnInfoEnum.SUCCESS, pageData);
    }

    /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = CustomizationException.class)
    @Override
    public ResponseResult deleteData(LinkedList<String> ids) {
        int i = opinionDao.deleteBatchIds(ids);

        if (i != ids.size()) {
            throw new CustomizationException(ReturnInfoEnum.DATA_FAIL);
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS);
    }
}
